import random
import pygame

from sprites_plane import *

# 未用到 先注掉
# j = []
# bomb_index = 0


class PlaneGame(object):
    def __init__(self):
        # 创建窗口
        self.screen = pygame.display.set_mode(SCREEN_RECT.size)
        self.clock = pygame.time.Clock()
        self.__create__sprites()
        # 创建敌机
        pygame.time.set_timer(CREATE_ENEMY_EVENT, 500)
        # 子弹发射频率
        pygame.time.set_timer(HERO_SHOOT_EVENT, 500)

    def __create__sprites(self):
        bg1 = Background()
        bg2 = Background(True)
        self.back_group = pygame.sprite.Group(bg1, bg2)

        self.enemy_group = pygame.sprite.Group()

        self.hero = Hero()
        self.hero_group = pygame.sprite.Group(self.hero)

        self.bomb_group = pygame.sprite.Group()
        self.hero_bomb_group = pygame.sprite.Group()

    def __event_handler(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                PlaneGame.__game__over()
            elif event.type == CREATE_ENEMY_EVENT:
                self.enemy = Enemy()
                self.enemy_group.add(self.enemy)
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                self.hero.fire()
        key_pressed = pygame.key.get_pressed()
        if key_pressed[pygame.K_RIGHT] or key_pressed[pygame.K_d]:
            self.hero.speed = 3
        elif key_pressed[pygame.K_LEFT] or key_pressed[pygame.K_a]:
            self.hero.speed = -3
        elif key_pressed[pygame.K_UP] or key_pressed[pygame.K_w]:
            self.hero.speed2 = -3
        elif key_pressed[pygame.K_DOWN] or key_pressed[pygame.K_s]:
            self.hero.speed2 = 3
        else:
            self.speed = 0
            self.speed2 = 0

    def __check__collide(self):
        bomb_enemies = pygame.sprite.groupcollide(self.enemy_group, self.hero.bullets, False, True)
        print(bomb_enemies)
        self.bomb_group.add(bomb_enemies)

        for enemy in self.bomb_group:
            if enemy.explode_index == 0:
                enemy.explode_index == 1
            elif enemy.explode_index == 5:
                self.enemy_group.remove_internal(enemy)
                self.bomb_group.remove_internal(enemy)

        bomb_hero = pygame.sprite.spritecollide(self.hero, self.enemy_group, True)
        if len(bomb_hero) > 0:
            self.hero.explode(self.screen)
            self.hero.kill()
            PlaneGame.__game__over()

    def __update__sprites(self):
        self.back_group.update()
        self.back_group.draw(self.screen)

        self.enemy_group.update()
        self.enemy_group.draw(self.screen)

        self.hero_group.update()
        self.hero_group.draw(self.screen)

        self.hero.bullets.update()
        self.hero.bullets.draw(self.screen)

    def start_game(self):
        while True:
            # 刷新帧率
            self.clock.tick(FRAME_PER_SEC)
            # 监控键盘事件
            self.__event_handler()
            # 检测碰撞
            self.__check__collide()
            # 更新精灵组
            self.__update__sprites()
            # 更新显示
            pygame.display.update()

    @staticmethod
    def __game__over():
        print("game over")
        pygame.quit()
        exit()


if __name__ == '__main__':
    game = PlaneGame()
    game.start_game()
